function post(path, postData, callback) {
    jQuery.ajaxSetup({
        'beforeSend': function(xhr) {
            xhr.setRequestHeader("userToken", userToken)
        }
    })
    $.post("/soasuite/mock/" + path, postData, function(data) {
        if (callback != null && "" != data) {
            callback($.parseJSON(data));
        }
    }).error(function() {
        userToken = null;
        window.location.reload();
    })
}

function get(path, params, callback) {
    jQuery.ajaxSetup({
        'beforeSend': function(xhr) {
            xhr.setRequestHeader("userToken", userToken)
        }
    })
    $.get("/soasuite/mock/", params, function(data) {
        if (callback != null && "" != data) {
            callback($.parseJSON(data));
        }
    }).error(function() {
        userToken = null;
        window.location.reload()
    });
}

function loadId(id, path) {
    var randomnumber = Math.floor(Math.random() * 10000);
    $("#" + id).load("/soasuite/content/" + path + ".html?cacheControl=" + randomnumber)

}

function loadContent(path) {
    loadId("content", path);
}

function loadLeft(path) {
    loadId("leftcol", path);
}

function setContentName(name) {
    $("#content_header").html(name);
}

function login() {

    var username = $("#username").val();
    var password = $("#password").val();

    post("login", {"u":username, "p":password}, loginHandler);
}

function enterKey(id, functionToCall) {
    $("#"+id).keyup(function(event) {
        if (event.keyCode == 13) {
            functionToCall();
        }
    });
}

function focus(id) {
    $("#"+id).focus();
}

function loginHandler(data) {
    var logged = true;
    if (data != null && data.auth == true) {
        userToken = data.userToken;
        loadContent("/user/home");
        loadLeft("tools");
    } else {
        $("#password").val("")
        $("#errorMessage").text(data.errorMessage);
    }
}